using System;
using FireStarter.Base;
using Firestarter.DB.Schema.BO;
using Habanero.Testability;
using NUnit.Framework;
using Firestarter.DB.Schema.Test.Logic;

namespace Firestarter.Test.Base.BusinessObjectMerger
{
    [TestFixture]
    public class TestBusinessObjectMerger : TestBase
    {
        // Test the constructor
        [Test]
        public void Test_Constructor()
        {
            //---------------Set up test pack-------------------
            DBDatabase dbDatabase = new DBDatabase();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(dbDatabase, null);
            //---------------Test Result -----------------------
            Assert.AreSame(dbDatabase, businessObjectMerger.ExistingMergableBo);
        }

        [Test]
        public void Test_Import_BothNull()
        {
            //---------------Set up test pack-------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(null, null);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            businessObjectMerger.Import(null);
            //---------------Test Result -----------------------
            //There should be no errors
            Assert.IsNull(businessObjectMerger.ExistingMergableBo);
        }

        [Test]
        public void Test_Import_ExistingIsNull()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(null, null);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, newDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, newDBDatabase.PendingChangeType);
            Assert.IsNull(businessObjectMerger.ExistingMergableBo);
            //---------------Execute Test ----------------------
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            //There should be no errors
            Assert.IsNotNull(businessObjectMerger.ExistingMergableBo);
            Assert.AreSame(newDBDatabase, businessObjectMerger.ExistingMergableBo);
            Assert.AreEqual(ChangeType.Add, newDBDatabase.PendingChangeType);
        }

        [Test]
        public void Test_Import_NewIsNull()
        {
            //---------------Set up test pack-------------------
            DBDatabase existingDBDatabase = new DBDatabase();
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, existingDBDatabase.PendingChangeType);
            //---------------Execute Test ----------------------
            businessObjectMerger.Import(null);
            //---------------Test Result -----------------------
            //There should be no errors
            Assert.IsNotNull(businessObjectMerger.ExistingMergableBo);
            Assert.AreSame(existingDBDatabase, businessObjectMerger.ExistingMergableBo);
            Assert.AreEqual(ChangeType.Delete, existingDBDatabase.PendingChangeType);
        }

        // Test the situation where both databases are empty
        [Test]
        public void Test_Import_BothDatabasesEmpty()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBDatabase existingDBDatabase = new DBDatabase();
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            //---------------Execute Test ----------------------
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
        }
       
        // Test the situation where a database has been updated
        [Test]
        public void Test_Import_Databases_Updated()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            newDBDatabase.DatabaseName = GetRandomString();
            newDBDatabase.Catalog = GetRandomString();
            newDBDatabase.Schema = GetRandomString();

            DBDatabase existingDBDatabase = new DBDatabase();
            existingDBDatabase.DatabaseName = GetRandomString();
            existingDBDatabase.Catalog = GetRandomString();
            existingDBDatabase.Schema = GetRandomString();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(newDBDatabase.DatabaseName, existingDBDatabase.DatabaseName);
            Assert.AreEqual(newDBDatabase.Catalog, existingDBDatabase.Catalog);
            Assert.AreEqual(newDBDatabase.Schema, existingDBDatabase.Schema);
        }

        // Test the situation where a database has been updated
        [Test]
        public void Test_Import_Databases_Same_PendingChangeTypeDiffers()
        {
            //---------------Set up test pack-------------------
            DBDatabase existingDBDatabase = new DBDatabase();
            existingDBDatabase.DatabaseName = GetRandomString();
            existingDBDatabase.Catalog = GetRandomString();
            existingDBDatabase.Schema = GetRandomString();
            existingDBDatabase.PendingChangeType = ChangeType.Add;

            DBDatabase newDBDatabase = new DBDatabase();
            newDBDatabase.DatabaseName = existingDBDatabase.DatabaseName;
            newDBDatabase.Catalog = existingDBDatabase.Catalog;
            newDBDatabase.Schema = existingDBDatabase.Schema;
            newDBDatabase.PendingChangeType = ChangeType.None;

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(ChangeType.Add, existingDBDatabase.PendingChangeType);
        }

        private string GetRandomString()
        {
            return RandomValueGen.GetRandomString();
        }

        // Test the situation where both databases have the same table
        [Test]
        public void Test_Import_DatabasesEqual_SameTable()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            string tableName = GetRandomString();
            DBTable newDBTable = new DBTable(tableName);
            newDBDatabase.Tables.Add(newDBTable);
            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(tableName);
            existingDBDatabase.Tables.Add(existingDBTable);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(existingDBTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Table has been Added
        [Test]
        public void Test_Import_TableAdded()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBDatabase existingDBDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(0, newDBDatabase.Tables.Count, "Table should have been moved");
            Assert.AreEqual(ChangeType.Add, dbTable.PendingChangeType);
        }

        // Test the situation where a Database Table has been Added
        [Test]
        public void Test_Import_TableAdded_WithColumns()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBColumn dbColumn = new DBColumn(GetRandomString());
            dbTable.Columns.Add(dbColumn);

            DBDatabase existingDBDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            Assert.AreEqual(1, dbTable.Columns.Count);
            Assert.AreEqual(ChangeType.None, dbColumn.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(0, newDBDatabase.Tables.Count, "Table should have been moved");
            Assert.AreEqual(ChangeType.Add, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbColumn.PendingChangeType);
        }

        // Test the situation where a Database Table has been Added
        [Test]
        public void Test_Import_TableAdded_WithPrimaryKey()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBPrimaryKey dbPrimaryKey = new DBPrimaryKey();
            dbTable.PrimaryKeyDB = dbPrimaryKey;

            DBDatabase existingDBDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.None, dbPrimaryKey.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(0, newDBDatabase.Tables.Count, "Table should have been moved");
            Assert.AreEqual(ChangeType.Add, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbPrimaryKey.PendingChangeType);
        }

        // Test the situation where a Database Table has been Added
        [Test]
        public void Test_Import_TableAdded_WithPrimaryKey_WithPKColumns()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBColumn dbColumn = new DBColumn(GetRandomString());
            dbTable.Columns.Add(dbColumn);
            DBPrimaryKey dbPrimaryKey = new DBPrimaryKey();
            dbTable.PrimaryKeyDB = dbPrimaryKey;
            DBPrimaryKeyColumn dbPrimaryKeyColumn = dbPrimaryKey.AddColumnInfo(dbColumn);

            DBDatabase existingDBDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.None, dbPrimaryKey.PendingChangeType);
            Assert.AreEqual(ChangeType.None, dbPrimaryKeyColumn.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(0, newDBDatabase.Tables.Count, "Table should have been moved");
            Assert.AreEqual(ChangeType.Add, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbPrimaryKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbPrimaryKeyColumn.PendingChangeType);
        }

        // Test the situation where a Database Table has been Added
        [Test]
        public void Test_Import_TableAdded_WithAlternateKey()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBKey dbKey = new DBKey { KeyName = GetRandomString() };
            dbTable.Keys.Add(dbKey);

            DBDatabase existingDBDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.None, dbKey.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(0, newDBDatabase.Tables.Count, "Table should have been moved");
            Assert.AreEqual(ChangeType.Add, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbKey.PendingChangeType);
        }

        // Test the situation where a Database Table has been Added
        [Test]
        public void Test_Import_TableAdded_WithAlternateKey_WithAKColumns()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBColumn dbColumn = new DBColumn(GetRandomString());
            dbTable.Columns.Add(dbColumn);
            DBKey dbKey = new DBKey { KeyName = GetRandomString() };
            dbTable.Keys.Add(dbKey);
            DBKeyColumn dbKeyColumn = dbKey.AddColumnInfo(dbColumn);

            DBDatabase existingDBDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.None, dbKey.PendingChangeType);
            Assert.AreEqual(ChangeType.None, dbKeyColumn.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(0, newDBDatabase.Tables.Count, "Table should have been moved");
            Assert.AreEqual(ChangeType.Add, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbKeyColumn.PendingChangeType);
        }

        // Test the situation where a Database Table has been Added
        [Test]
        public void Test_Import_TableAdded_WithForeignKey()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBForeignKey dbForeignKey = new DBForeignKey();
            dbTable.ForeignKeys.Add(dbForeignKey);

            DBDatabase existingDBDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.None, dbForeignKey.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(0, newDBDatabase.Tables.Count, "Table should have been moved");
            Assert.AreEqual(ChangeType.Add, dbTable.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbForeignKey.PendingChangeType);
        }

        // Test the situation where a Database Table has been Deleted
        [Test]
        public void Test_Import_TableDeleted()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            existingDBDatabase.Tables.Add(dbTable);

            //---------------Assert Precondition----------------
            Assert.AreEqual(0, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, dbTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(dbTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(ChangeType.Delete, dbTable.PendingChangeType);
        }

        // Test the situation where a Database Table has been Updated
        [Test]
        public void Test_Import_TableUpdated()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            string tableName = GetRandomString();
            DBTable newDBTable = new DBTable(tableName);
            string newTableComment = GetRandomString();
            newDBTable.TableComment = newTableComment;
            newDBDatabase.Tables.Add(newDBTable);
            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(tableName);
            existingDBTable.TableComment = GetRandomString();
            existingDBDatabase.Tables.Add(existingDBTable);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(existingDBTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(newTableComment, existingDBTable.TableComment);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Table has been Updated
        [Test]
        public void Test_Import_TableUnchanged()
        {
            //---------------Set up test pack-------------------
            string tableName = GetRandomString();
            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(tableName)
            {
                TableComment = GetRandomString()
            };
            existingDBDatabase.Tables.Add(existingDBTable);

            DBDatabase newDBDatabase = new DBDatabase();
            DBTable newDBTable = new DBTable(tableName)
            {
                TableComment = existingDBTable.TableComment
            };
            newDBDatabase.Tables.Add(newDBTable);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(existingDBTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Table has been Updated
        [Test]
        public void Test_Import_TableUnchanged_WithIgnoredPropDifferent()
        {
            //---------------Set up test pack-------------------
            string tableName = GetRandomString();
            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(tableName)
            {
                TableComment = GetRandomString(),
                Approved = true
            };
            existingDBDatabase.Tables.Add(existingDBTable);

            DBDatabase newDBDatabase = new DBDatabase();
            DBTable newDBTable = new DBTable(tableName)
            {
                TableComment = existingDBTable.TableComment,
                Approved = false
            };
            newDBDatabase.Tables.Add(newDBTable);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, prop => prop.PropertyName == "Approved");
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreSame(existingDBTable, existingDBDatabase.Tables[0]);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Column has been Added
        [Test]
        public void Test_Import_SameTable_ColumnAdded()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBColumn dbColumn = new DBColumn(GetRandomString());
            dbTable.Columns.Add(dbColumn);

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].Columns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables[0].Columns.Count);
            Assert.AreEqual(ChangeType.None, dbColumn.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBTable.Columns.Count);
            Assert.AreSame(dbColumn, existingDBTable.Columns[0]);
            Assert.AreEqual(0, dbTable.Columns.Count, "Column should have been moved");
            Assert.AreEqual(ChangeType.Add, dbColumn.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Column has been Deleted
        [Test]
        public void Test_Import_SameTable_ColumnDeleted()
        {
            //---------------Set up test pack-------------------
            DBDatabase newdatabase = new DBDatabase();
            DBTable newtable = new DBTable(GetRandomString());
            newdatabase.Tables.Add(newtable);

            DBDatabase existingdatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(newtable.TableName);
            existingdatabase.Tables.Add(existingDBTable);
            DBColumn existingcolumn = new DBColumn(GetRandomString());
            existingDBTable.Columns.Add(existingcolumn);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newdatabase.Tables.Count);
            Assert.AreEqual(0, newdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(1, existingdatabase.Tables.Count);
            Assert.AreEqual(1, existingdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(ChangeType.None, existingcolumn.PendingChangeType);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingdatabase, null);
            businessObjectMerger.Import(newdatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newdatabase.Tables.Count);
            Assert.AreEqual(0, newdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(1, existingdatabase.Tables.Count);
            Assert.AreEqual(1, existingdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(ChangeType.Delete, existingcolumn.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Column has been Updated
        [Test]
        public void Test_Import_SameTable_ColumnUpdated()
        {
            //---------------Set up test pack-------------------

            DBDatabase newdatabase = new DBDatabase();
            DBTable newtable = new DBTable(GetRandomString());
            newdatabase.Tables.Add(newtable);
            DBColumn newDBColumn = new DBColumn(GetRandomString());
            newtable.Columns.Add(newDBColumn);
            string newColumnComment = GetRandomString();
            newDBColumn.Comment = newColumnComment;

            DBDatabase existingdatabase = new DBDatabase();
            DBTable existingtable = new DBTable(newtable.TableName);
            existingdatabase.Tables.Add(existingtable);
            DBColumn existingDBColumn = new DBColumn(newDBColumn.ColumnName);
            existingtable.Columns.Add(existingDBColumn);
            existingDBColumn.Comment = GetRandomString();

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newdatabase.Tables.Count);
            Assert.AreEqual(1, newdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(1, existingdatabase.Tables.Count);
            Assert.AreEqual(1, existingdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(ChangeType.None, existingDBColumn.PendingChangeType);
            Assert.AreEqual(ChangeType.None, existingtable.PendingChangeType);

            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingdatabase, null);
            businessObjectMerger.Import(newdatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newdatabase.Tables.Count);
            Assert.AreEqual(1, newdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(1, existingdatabase.Tables.Count);
            Assert.AreEqual(1, existingdatabase.Tables[0].Columns.Count);
            Assert.AreEqual(newColumnComment, existingDBColumn.Comment);
            Assert.AreEqual(ChangeType.Update, existingtable.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBColumn.PendingChangeType);
        }

        // Test the situation where a Database Key has been Added
        [Test]
        public void Test_Import_SameTable_KeyAdded()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBKey dbKey = new DBKey{KeyName = GetRandomString()};
            dbTable.Keys.Add(dbKey);

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(ChangeType.None, dbKey.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBTable.Keys.Count);
            Assert.AreSame(dbKey, existingDBTable.Keys[0]);
            Assert.AreEqual(0, dbTable.Keys.Count, "DBKey should have been moved");
            Assert.AreEqual(ChangeType.Add, dbKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Key has been Deleted
        [Test]
        public void Test_Import_SameTable_KeyDeleted()
        {
            //---------------Set up test pack-------------------
            DBDatabase newdatabase = new DBDatabase();
            DBTable newtable = new DBTable(GetRandomString());
            newdatabase.Tables.Add(newtable);

            DBDatabase existingdatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(newtable.TableName);
            existingdatabase.Tables.Add(existingDBTable);
            DBKey existingDBKey = new DBKey { KeyName = GetRandomString() };
            existingDBTable.Keys.Add(existingDBKey);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newdatabase.Tables.Count);
            Assert.AreEqual(0, newdatabase.Tables[0].Keys.Count);
            Assert.AreEqual(1, existingdatabase.Tables.Count);
            Assert.AreEqual(1, existingdatabase.Tables[0].Keys.Count);
            Assert.AreEqual(ChangeType.None, existingDBKey.PendingChangeType);
            Assert.AreEqual(ChangeType.None, existingDBTable.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingdatabase, null);
            businessObjectMerger.Import(newdatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newdatabase.Tables.Count);
            Assert.AreEqual(0, newdatabase.Tables[0].Keys.Count);
            Assert.AreEqual(1, existingdatabase.Tables.Count);
            Assert.AreEqual(1, existingdatabase.Tables[0].Keys.Count);
            Assert.AreEqual(ChangeType.Delete, existingDBKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Key Column has been Added
        [Test]
        public void Test_Import_SameTable_KeyColumnAdded()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBKey dbKey = new DBKey { KeyName = GetRandomString() };
            dbTable.Keys.Add(dbKey);
            DBKeyColumn dbKeyCol = new DBKeyColumn ();
            dbKey.KeyColumns.Add(dbKeyCol);

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            DBKey existingDBKey = new DBKey { KeyName = dbKey.KeyName };
            existingDBTable.Keys.Add(existingDBKey);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].Keys[0].KeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables[0].Keys[0].KeyColumns.Count);
            Assert.AreEqual(ChangeType.None, dbKeyCol.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].Keys[0].KeyColumns.Count,"This is expected to be moved");
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].Keys[0].KeyColumns.Count);

            Assert.AreEqual(ChangeType.Add, dbKeyCol.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Key Column has been Deleted
        [Test]
        public void Test_Import_SameTable_KeyColumnDeleted()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBKey dbKey = new DBKey { KeyName = GetRandomString() };
            dbTable.Keys.Add(dbKey);

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            DBKey existingDBKey = new DBKey { KeyName = dbKey.KeyName };
            existingDBTable.Keys.Add(existingDBKey);
            DBKeyColumn dbKeyCol = new DBKeyColumn();
            existingDBKey.KeyColumns.Add(dbKeyCol);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].Keys[0].KeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].Keys[0].KeyColumns.Count);
            Assert.AreEqual(ChangeType.None, dbKeyCol.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].Keys[0].KeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].Keys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].Keys[0].KeyColumns.Count);

            Assert.AreEqual(ChangeType.Delete, dbKeyCol.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Primary Key has been Added
        [Test]
        public void Test_Import_SameTable_PrimaryKeyAdded()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBPrimaryKey dbPrimaryKey = new DBPrimaryKey();
            dbTable.PrimaryKeyDB = dbPrimaryKey;

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.IsNotNull(dbTable.PrimaryKeyDB);
            Assert.IsNotNull(newDBDatabase.Tables[0].PrimaryKeyDB);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.IsNull(existingDBTable.PrimaryKeyDB);
            Assert.AreEqual(ChangeType.None, dbPrimaryKey.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.IsNotNull(existingDBTable.PrimaryKeyDB);
            Assert.AreSame(dbPrimaryKey, existingDBTable.PrimaryKeyDB);
            Assert.IsNull(dbTable.PrimaryKeyDB, "DBKey should have been moved");
            Assert.AreEqual(ChangeType.Add, dbPrimaryKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        [Test]
        public void Test_SetPrimaryKey()
        {
            //---------------Set up test pack-------------------
            DBTable dbTable = new DBTable(GetRandomString());
            DBPrimaryKey dbPrimaryKey = new DBPrimaryKey();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dbPrimaryKey);
            //---------------Execute Test ----------------------
            dbTable.PrimaryKeyDB = dbPrimaryKey;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dbTable.PrimaryKeyDB, "Primary key should not be null");
            Assert.AreSame(dbPrimaryKey, dbTable.PrimaryKeyDB);
        }
        // Test the situation where a Primary Key has been Deleted
        [Test]
        public void Test_Import_SameTable_PrimaryKeyDeleted()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            DBPrimaryKey dbPrimaryKey = new DBPrimaryKey();
            existingDBTable.PrimaryKeyDB = dbPrimaryKey;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.IsNull(newDBDatabase.Tables[0].PrimaryKeyDB);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.IsNotNull(existingDBTable.PrimaryKeyDB);
            Assert.AreEqual(ChangeType.None, dbPrimaryKey.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.IsNotNull(existingDBTable.PrimaryKeyDB);
            Assert.AreSame(dbPrimaryKey, existingDBTable.PrimaryKeyDB);
            Assert.AreEqual(ChangeType.Delete, dbPrimaryKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        //// Test the situation where a Database Primary Key Column has been Added (PK Changed)
        [Test]
        public void Test_Import_SameTable_PrimaryKeyColumnAdded()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            dbTable.PrimaryKeyDB = new DBPrimaryKey();
            DBPrimaryKeyColumn dbPrimaryKeyColumn = new DBPrimaryKeyColumn();
            dbTable.PrimaryKeyDB.PrimaryKeyColumns.Add(dbPrimaryKeyColumn);

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            existingDBTable.PrimaryKeyDB = new DBPrimaryKey();
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            //businessObjectMerger.AddSingleRelationshipToTraverse(typeof(Table), "DBPrimaryKey");
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count);
            Assert.AreEqual(ChangeType.None, dbPrimaryKeyColumn.PendingChangeType);
            //---------------Execute Test ----------------------
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count, "This is expected to be moved");
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count);

            Assert.AreEqual(ChangeType.Add, dbPrimaryKeyColumn.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PrimaryKeyDB.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Database Primary Key Column has been Deleted (PK Changed)
        [Test]
        public void Test_Import_SameTable_PrimaryKeyColumnDeleted()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            dbTable.PrimaryKeyDB = new DBPrimaryKey();

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            existingDBTable.PrimaryKeyDB = new DBPrimaryKey();
            DBPrimaryKeyColumn dbPrimaryKeyColumn = new DBPrimaryKeyColumn();
            existingDBTable.PrimaryKeyDB.PrimaryKeyColumns.Add(dbPrimaryKeyColumn);

            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            //businessObjectMerger.AddSingleRelationshipToTraverse(typeof(Table), "DBPrimaryKey");
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count);
            Assert.AreEqual(ChangeType.None, dbPrimaryKeyColumn.PendingChangeType);
            //---------------Execute Test ----------------------
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].PrimaryKeyDB.PrimaryKeyColumns.Count);

            Assert.AreEqual(ChangeType.Delete, dbPrimaryKeyColumn.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PrimaryKeyDB.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        // Test the situation where a Foreign Key Column has been Added
        [Test]
        public void Test_Import_SameTable_ForeignKey_ColumnAdded()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBForeignKey dbForeignKey = new DBForeignKey();
            dbTable.ForeignKeys.Add(dbForeignKey);
            DBForeignKeyColumn foreignkeyCol = new DBForeignKeyColumn();
            dbForeignKey.ForeignKeyColumns.Add(foreignkeyCol);

            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            DBForeignKey existingDBForeignKey = new DBForeignKey();
            existingDBTable.ForeignKeys.Add(existingDBForeignKey);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(0, existingDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);
            Assert.AreEqual(ChangeType.None, foreignkeyCol.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count, "This is expected to be moved");
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);

            Assert.AreEqual(ChangeType.Add, foreignkeyCol.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBForeignKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        //// Test the situation where a Database ForeignKey Column has been Deleted
        [Test]
        public void Test_Import_SameTable_ForeignKey_ColumnDeleted()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBForeignKey dbForeignKey = new DBForeignKey();
            dbTable.ForeignKeys.Add(dbForeignKey);


            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            DBForeignKey existingDBForeignKey = new DBForeignKey();
            existingDBTable.ForeignKeys.Add(existingDBForeignKey);
            DBForeignKeyColumn foreignkeyCol = new DBForeignKeyColumn();
            existingDBForeignKey.ForeignKeyColumns.Add(foreignkeyCol);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);
            Assert.AreEqual(ChangeType.None, foreignkeyCol.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(0, newDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);

            Assert.AreEqual(ChangeType.Delete, foreignkeyCol.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBForeignKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

        //// Test the situation where a  ForeignKey Column has been changed
        [Test, Ignore]// Mark to help here????
        public void Test_Import_SameTable_ForeignKey_ColumnChanged()
        {
            //---------------Set up test pack-------------------
            DBDatabase newDBDatabase = new DBDatabase();
            DBTable dbTable = new DBTable(GetRandomString());
            newDBDatabase.Tables.Add(dbTable);
            DBForeignKey dbForeignKey = new DBForeignKey();
            dbTable.ForeignKeys.Add(dbForeignKey);
            DBForeignKeyColumn foreignkeyCol = new DBForeignKeyColumn();
            DBForeignKeyColumn newForeignkeyCol = new DBForeignKeyColumn();
            dbForeignKey.ForeignKeyColumns.Add(foreignkeyCol);
            dbForeignKey.ForeignKeyColumns.Add(newForeignkeyCol);
            
            DBDatabase existingDBDatabase = new DBDatabase();
            DBTable existingDBTable = new DBTable(dbTable.TableName);
            existingDBDatabase.Tables.Add(existingDBTable);
            DBForeignKeyColumn existingForeignkeyCol = new DBForeignKeyColumn();
            DBForeignKey existingDBForeignKey = new DBForeignKey();
            existingDBTable.ForeignKeys.Add(existingDBForeignKey);
            existingDBForeignKey.ForeignKeyColumns.Add(existingForeignkeyCol);

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(2, newDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);
            Assert.AreEqual(ChangeType.None, foreignkeyCol.PendingChangeType);
            //---------------Execute Test ----------------------
            FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger businessObjectMerger = new FireStarter.Base.BusinessObjectMerger.BusinessObjectMerger(existingDBDatabase, null);
            businessObjectMerger.Import(newDBDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, newDBDatabase.Tables.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(1, newDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count, "OneColumn Info Moved");
            Assert.AreEqual(1, existingDBDatabase.Tables.Count);
            Assert.AreEqual(1, existingDBDatabase.Tables[0].ForeignKeys.Count);
            Assert.AreEqual(2, existingDBDatabase.Tables[0].ForeignKeys[0].ForeignKeyColumns.Count);

            Assert.AreEqual(ChangeType.Delete, foreignkeyCol.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBForeignKey.PendingChangeType);
            Assert.AreEqual(ChangeType.Update, existingDBTable.PendingChangeType);
        }

  
    }
}